/**
 *
 * @author Valdez
 */
public class HuffmanTree implements Comparable<HuffmanTree>{
    
    BinaryTree<NodeHuffman> root;
    int totalWeight;
    
    //post: contruye un nodo asociado al caracter
    public  HuffmanTree(NodeHuffman e){
        root = new BinaryTree(e);
        totalWeight = e.frequency;
    }
    
    //pre: left y right no son null
    //post: une dos arboles y une sus frecuencias
    public HuffmanTree(HuffmanTree left, HuffmanTree right){
        root = new BinaryTree(null, left.root, right.root);
        totalWeight = left.totalWeight + right.totalWeight;
    }
    
    //pre: other no es null
    //post: devuelve un entero reflejando la relación entre los valores
    @Override
    public int compareTo(HuffmanTree other){
        int temp = 0;
        if (this.totalWeight<other.totalWeight)
            temp = -1;
        if (this.totalWeight>other.totalWeight)
            temp = 1;
        if (this.totalWeight==other.totalWeight)
            temp = 0;
        return temp;
    }
    
    // post: devuelve true si this y that son iguales
    public boolean equals(HuffmanTree that){
        return (this == that);
    }

    // post: imprime un string asociado con el caracter en el arbol
    public void print(){
        print(this.root, "");
    }

    // post: imprime un string asociado con el caracter en r
    // prefijo por representation
    protected void print(BinaryTree r, String representation){
        if (r.getLeft() != null){
            print(r.getLeft(), representation+"0");
            print(r.getRight(), representation+"1");
        }
        else{
            NodeHuffman temp = (NodeHuffman)r.getValue();
            if (temp != null)
                System.out.println("Caracter: "+temp.ch+ " Frecuencia: "+temp.frequency+" Codigo:"+representation);
        }
    }
    
}
